<?php 
class Shipping
{
	public $Methods;
	public $SiteMethods;
	public $Areas;
	public $Zones;
	public $UserZip;
	
	public function Shipping($userDetails, $cart) 
	{
		if(is_object($userDetails))
		{ self::setMethods($userDetails); }
		if(($cart->getItems() > 0) && ($this->SiteMethods))
		{ self::setMethodPrices($cart); }
		if($this->SiteMethods)
		{ self::makeMethods(); }
	}
	
	public function getAreas() { return $this->Areas; }
	public function getZones() { return $this->Zones; }
	public function getMethods() { return $this->Methods; }
	public function getMethod($id=0) 
	{ 
		if(isset($this->Methods[$id]))
		{ return $this->Methods[$id]; }
	}
	
	function makeMethods()
	{
		if($this->SiteMethods)
		{
			foreach($this->SiteMethods as $method)
			{ $this->Methods[$method['id']] = $method; }
		}
	}
	
	function setMethods($userDetails)
	{
		$where = '';
		$zip = 0;
		$country = '';
		
		if($userDetails->getAlternateDelivery() > 0)
		{ 
			$zip = $userDetails->getDeliveryZip(); 
			$country  = $userDetails->getDeliveryCountry(); 
		}
		else
		{ 
			$zip = $userDetails->getZip(); 
			$country = $userDetails->getCountry(); 
		}
		
		if($userDetails->getAlternateDelivery() > 0)
		{ $zip = $userDetails->getDeliveryZip(); }
		else
		{ $zip = $userDetails->getZip(); }
		
		if($zip > 0)
		{ $where = ' AND ('.$zip.' BETWEEN a.zip_start AND a.zip_end)'; }	
		else
		{ $where = ' AND (a.zip_start=0 AND a.zip_end>=9999)'; }	
		
		if($country != '')
		{ $where .= 'AND ((z.country="'.$country.'") OR (z.country="0"))'; }	
		
		$select  = 'SELECT s.*, a.zone_id, a.city, a.id AS area_id, z.code, z.title AS zone_title, z.email FROM '.DB_PERFIX.'shop_shipping_areas a, '.DB_PERFIX.'shop_shipping_zones z, '.DB_PERFIX.'shop_shipping s ';
		$select .= 'WHERE a.zone_id=z.id AND s.id=z.parent_id AND s.active=1 AND (s.site_id='.SITE_ID.' OR s.site_id=0) '.$where.' ORDER BY zone_title ASC;';
		$result = mysql_query($select);
		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_array($result))
			{
				$this->SiteMethods[$row['id']]['id'] = $row['id'];
				$this->SiteMethods[$row['id']]['title'] = $row['title'];
				$this->SiteMethods[$row['id']]['Description'] = $row['Description'];
				$this->SiteMethods[$row['id']]['RemoteId'] = $row['RemoteId'];
				$this->SiteMethods[$row['id']]['tax_id'] = $row['tax_id'];
				$this->SiteMethods[$row['id']]['FreeShippingDateActiveStart'] 	= $row['FreeShippingDateActiveStart'];
				$this->SiteMethods[$row['id']]['FreeShippingDateActiveEnd'] 	= $row['FreeShippingDateActiveEnd'];
				$this->SiteMethods[$row['id']]['complete_status'] 	= $row['complete_status'];
				$this->SiteMethods[$row['id']]['zone_id'] = $row['zone_id'];
				$this->SiteMethods[$row['id']]['price'] = 0;
				
				$this->Areas[$row['id']][$row['area_id']]['city'] 	= $row['city'];
				
				$this->Zones[$row['id']][$row['zone_id']]['id'] 	= $row['zone_id'];
				$this->Zones[$row['id']][$row['zone_id']]['title'] 	= $row['zone_title'];
				$this->Zones[$row['id']][$row['zone_id']]['code'] 	= $row['code'];
				$this->Zones[$row['id']][$row['zone_id']]['email'] 	= $row['email'];
			}
		}
	}
	
	function setMethodPrices($cart)
	{
		if($this->SiteMethods)
		{
			$wheresz = '';
			foreach($this->SiteMethods as $method)
			{
				if($wheresz != '')
				{ $wheresz .= ' OR '; }
				$wheresz .= '(shipping_id='.$method['id'].' AND (zone_id='.$method['zone_id'].' OR zone_id=0))';
			}
			$where = '';
			if($wheresz != '')
			{ 
				if(intval($cart->cartGrosWeight) > 0)
				{ $where .= ' AND (('.$cart->cartGrosWeight.' BETWEEN min_weight AND max_weight) OR (min_weight > 0 AND min_weight < '.$cart->cartGrosWeight.' AND max_weight=0))'; }	
				else
				{ $where .= ' AND ((`min_weight` = 0 AND `max_weight` >= '.intval($cart->cartGrosWeight).'))'; }	
				
				if(intval($cart->cartVolume) > 0)
				{ $where .= ' AND (('.$cart->cartVolume.' BETWEEN min_volume AND max_volume) OR (min_volume > 0 AND min_volume < '.$cart->cartVolume.' AND max_volume=0))'; }
				else
				{ $where .= ' AND ((`min_volume` = 0 AND `max_volume` >= '.intval($cart->cartVolume).'))'; }
				
				$select  = 'SELECT * FROM '.DB_PERFIX.'shop_shipping_rules WHERE ('.$wheresz.') '.$where.' ;'; 
				//echo $select;
				$result = mysql_query($select);
				if(mysql_num_rows($result) > 0)
				{
					while($row = mysql_fetch_array($result))
					{ 
						if((strtotime($this->SiteMethods[$row['shipping_id']]['FreeShippingDateActiveStart']) > 0) && 
					       (strtotime($this->SiteMethods[$row['shipping_id']]['FreeShippingDateActiveStart']) < time()) && 
					       (strtotime($this->SiteMethods[$row['shipping_id']]['FreeShippingDateActiveEnd']) > time()))
						{
							$this->SiteMethods[$row['shipping_id']]['price'] = 0; 
							$this->SiteMethods[$row['shipping_id']]['price-original'] = $row['price']; 
						}
						else
						{
							$this->SiteMethods[$row['shipping_id']]['price'] = $row['price']; 
						}
					}
				}
			}
		}
	}
	
	function getMethodMindate($methodId=0)
	{
		$weekdayNow = date("N");
		$hourNow = date("G");
		$minuteNow = intval(date("i"));
		//$hourNow = 11;
		if($methodId == 5) //Weekend delivery
		{
			if($weekdayNow == 1)
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+5,date("Y")); }
			if($weekdayNow == 2)
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+4,date("Y")); }
			if($weekdayNow == 3)
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+3,date("Y")); }
			
			if($weekdayNow == 4) 
			{ 
				if(($hourNow > 11) || (($hourNow = 11) && ($minuteNow > 45))) 	//Thursday after 12 delivery next saturday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+9,date("Y")); }
				else															//Thursday before 12 delivery saturday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+2,date("Y")); }
				/*
				if(($hourNow < 12) && ($minuteNow < 45)) 	//Thursday before 12 delivery saturday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+2,date("Y")); }
				else 				//Thursday after 12 delivery next saturday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+9,date("Y")); }*/
			}
			
			if($weekdayNow == 5) //Friday -> Pickup next saturday
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+8,date("Y")); }
			if($weekdayNow == 6) //Saturday -> Pickup next saturday
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+7,date("Y")); }
			if($weekdayNow == 7) //Sunday -> Pickup next saturday
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+6,date("Y")); }
		}
		else
		{
			if($weekdayNow < 4) 
			{ 
				//if(($hourNow < 12) && ($minuteNow < 45)) 	//Monday-Wedensday before 11:45 pickup next day
				if(($hourNow > 11) || (($hourNow = 11) && ($minuteNow > 45)))	//Monday-Wedensday after 12 pickup in 2 days
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+2,date("Y")); }
				else 															//Monday-Wedensday before 11:45 pickup next day
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+1,date("Y")); }
				//else 				//Monday-Wedensday after 12 pickup in 2 days
				//{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+2,date("Y")); }
			}
			if($weekdayNow == 4) //Thursday
			{ 
				if(($hourNow > 11) || (($hourNow = 11) && ($minuteNow > 45))) 	//Thursday after 12 pickup monday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+4,date("Y")); }
				else 															//Thursday before 12 pickup pickup next day
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+1,date("Y")); }
			}
			if($weekdayNow == 5) //Friday -> Pickup monday
			{ 
				if(($hourNow > 11) || (($hourNow = 11) && ($minuteNow > 45))) 	//Friday after 12 pickup tuesday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+4,date("Y")); }
				else														 	//Friday before 12 pickup monday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+3,date("Y")); }
				/*
				if(($hourNow < 12) && ($minuteNow < 45)) 	//Friday before 12 pickup monday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+3,date("Y")); }
				else 				//Friday after 12 pickup tuesday
				{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+4,date("Y")); }*/
			}
			if($weekdayNow == 6) //Saturday -> Pickup tuesday
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+3,date("Y")); }
			if($weekdayNow == 7) //Sunday -> Pickup tuesday
			{ $mindate = mktime(date("h"),0,0,date("m"),date("d")+2,date("Y")); }
		}
		return $mindate;
	}
}
?>
